﻿
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="th">
<meta name="viewport" content="width=1090" />
<title>Elderly Society</title>

<!-- CSS Framework -->
<link rel="stylesheet" href=<?php echo base_url()."css/screen.css"?> type="text/css" media="screen, projection">
<link rel="stylesheet" href=<?php echo base_url()."css/jquery.jgrowl.css"?> type="text/css" media="screen, projection">
<!--<link rel='stylesheet' type='text/css' href=<?php echo base_url()."css/fullcalendar.css"?> /> 
<link rel='stylesheet' type='text/css' href=<?php echo base_url()."css/fullcalendar.print.css'"?> media='print' /> -->
<script src=<?php echo base_url()."js/jquery-1.6.min.js"?> type="text/javascript" charset="utf-8"></script>
<!--<script src=<?php echo base_url()."js/fullcalendar.js"?> type="text/javascript" charset="utf-8"></script> -->
<script src=<?php echo base_url()."js/jquery.periodicalupdater.js"?> type="text/javascript" charset="utf-8"></script>
<script src=<?php echo base_url()."js/jquery.jgrowl.js"?> type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
	$(document).ready(function(){
		var mid="";
		var tid="";
		$('a#add').click(function(){
			$('div#med_add').show();
		});
		
		$('#add_b').click(function(){
			if($('#type').val() == "ยาทา"){
				var unit = "";
			}else{
				if($('label#unit').text() != ""){
					var unit = $('label#unit').text();
				}
				else{
					var unit = $('select#unit').val();
				}
			}
			if($('#type').val() == "ยาเม็ด"){
				var total = $('#total_dose').val();
			}
			else{
				var total = 0;
			}
			
			var time = "";
			$(':checkbox:checked').each(function(i){
				time += $(this).val() + ',';
			});
			var med_data = {
				'name' : $('#med_name').val(),
				'routine' : $('#routine').val(),
				'time' : time,
				'type' : $('#type').val(),
				'dose' : $('#dose_input').val(),
				'unit' : unit,
				'total' : total,
				'detail' : $('textarea#detail').val()
			}
			$.ajax({
				url: "<?php echo base_url(); ?>index.php/profile/add_med_noti",
				type: "POST",
				data: med_data,
				success: function(data){
					window.location = "<?php echo base_url(); ?>index.php/profile/med_taking";
					//alert(data);
				}
			});
			//alert(unit);
		});
		
		$('#routine').change(function(){
			if($(this).val()=="Every...Hour(s)"){
				$('#time').hide();
				$('#time_text').show();
			}
			else if($(this).val()=="---SELECT ROUTINE---" || $(this).val()=="เมื่อมีอาการ"){
				$('#time_text').hide();
				$('#time').hide();
			}
			else{
				$('#time').show();
				$('#time_text').hide();
			}
		});
		
		$('#type').change(function(){
			if($(this).val()=="ยาทา" || $(this).val()=="---SELECT TYPE---") {
				$('#dose').hide();
			}
			else{
				$('#dose').show();
			}
			switch($(this).val()){
				case "ยาเม็ด" : $('label#unit').html('เม็ด'); $('select#unit').hide(); $('#total').show(); break;
				case "ยาน้ำ" : $('select#unit').show(); $('label#unit').html(''); $('#total').hide(); 	break;
				case "ยาพ่น" : $('label#unit').html('ครั้ง'); $('select#unit').hide(); $('#total').hide();  break;
				case "ยาฉีด" : $('label#unit').html('เข็ม'); $('select#unit').hide(); $('#total').hide();  break;
				case "ยาหยอด" : $('label#unit').html('หยด'); $('select#unit').hide(); $('#total').hide();  break;
			}
		});
		
		$.PeriodicalUpdater({
			url:"<?php echo base_url(); ?>index.php/home/test",
			method: 'post',
			minTimeout: 60000,
			maxTimeout: 7000,
			type: 'JSON'
		  },
		  function(data){
			if(data!=0){
				if(data.notify==1){
					var med_text = "";
					mid = '';
					tid = '';
					$.each(data.medicines, function(i, med){
						mid += med.med_id + ',';
						tid += med.time_id + ',';
						med_text = "<div>"
							+ "<label>"+med.med_name+" "+"</label>"
							+ "<label>"+med.dose+" "+med.unit+"</label>"
							+ "</div>";
						$.jGrowl("Time to take medicine"+med_text, {
							life: 15000,
							position: 'center'
						});
						
									
					});
				}
				else if(data.notify==2){
					var button = '<br/><input id="yes" type="button" value="Yes" />'
								+'<input id="no" type="button" value="No" />';
					$.jGrowl("Did you take the medicine"+button, {
						life: 30000,
						position: 'center'
					});
				}
				else if(data.notify==3){
					var button = '<br/><input id="yes" type="button" value="Yes" />'
								+'<input id="no" type="button" value="No" />';
					$.jGrowl("Did you take the medicine"+button, {
						life: 30000,
						position: 'center',
						close: function(e,m){
							var med_change = {
								'med_id' : mid,
								'time_id' : tid
							}
							$.ajax({
								url: "<?php echo base_url(); ?>index.php/profile/unconfirmed",
								type: "POST",
								data: med_change,
								success: function(data){		
									alert("Confirmed!!");
								}
							});
						}
					});
				}
				
				
				
			}
		  }
		);
		
		$('#yes').live('click',function(){
			var confirmed_med = {
				'med_id' : mid,
				'time_id' : tid
			}
			
			$.ajax({
				url: "<?php echo base_url(); ?>index.php/profile/confirmed",
				type: "POST",
				data: confirmed_med,
				success: function(data){		
					alert("Confirmed!!");
				}
			});
		});
		$('#no').live('click',function(){
			alert('No!!');
		});
		
		$('input#edit').click(function(){
			window.location = "<?php echo base_url();?>index.php/profile/edit_page";
		});
	});
</script>
</head>
<body>
<div id="whole">
	<!-- Header Section -->
	<header class="container">
		<div class="prepend-19 span-5 last">
			<?php if($this->session->userdata('logged_in')): ?>
			<a href="<?php echo base_url(); ?>index.php/home/logout"><img src="<?php echo base_url();?>img/signout.png" /></a>
			<?php endif;?>
		</div>
		<div class="span-7">
			<!-- LOGO -->
			<div id="logo">
				<a href="<?php echo base_url(); ?>"><img src="<?php echo base_url();?>img/logo.png" /></a>
			</div>
		</div>
		
		<div class="prepend-2 span-15 last">
			<!-- 3 Section (SOCIAL, HEALTH, IT TIPS) -->
			<nav id="main-nav">
				<ul>
					<li class="main-nav"><a href="<?php echo base_url(); ?>">HOME</a></li>
					<li class="main-nav"><a href="<?php echo base_url(); ?>index.php/cal">CALENDAR</a></li>
					<li class="main-nav"><a href="<?php echo base_url(); ?>index.php/profile">PROFILE</a></li>
				</ul>
			</nav>
		</div>
		<div><img class="span-24 last" src="<?php echo base_url();?>img/blackbar.png" /></div>
		<div class="span-24 last" id="flash_header">
			<img src="<?php echo base_url();?>img/header_2.jpg" />
		</div>
	</header>
	
	<!-- Main Section -->
	<div id="main-section" class="container ">
		<!-- Navigation Button -->
		<div  class="span-5">
			<nav id="nav">
				<ul>
					<li><a href="<?php echo base_url(); ?>index.php/profile/profile_view"><img src="<?php echo base_url(); ?>img/profile_th.png" /></a></li>
					<li><a href="<?php echo base_url(); ?>index.php/profile/med_taking"><img src="<?php echo base_url(); ?>img/med_taking_th.png" /></a></li>
					<li><a href="<?php echo base_url(); ?>index.php/profile/med_report"><img src="<?php echo base_url(); ?>img/med_report_th.png" /></a></li>
				</ul>
			</nav>
		</div>